﻿using BXH.Movie.Model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BXH.Movie.Dal
{
    public class MovieSessionDal : BaseConnection
    {
        public bool Add(MovieSession movieSession)
        {
            string sql = "insert into MovieSession values(@MovieId,@BroadcastTime)";
            return Conn.Execute(sql, movieSession) > 0;
        }
        public bool Update(MovieSession movieSession)
        {
            string sql = @"Update  MovieSession set 
                            MovieId=@MovieId,
                            BroadcastTime=@BroadcastTime 
                            where Id=@Id";
            return Conn.Execute(sql, movieSession) > 0;
        }
        public IEnumerable<MovieSession> GetSessionByMovieId(int id)
        {
            string sql = "select *from MovieSession where MovieId=@MovieId";
            return Conn.Query<MovieSession>(sql, new { MovieId = id });
        }

        public IEnumerable<MovieSessionInfo> GetAllSession()
        {
            string sql = @"select  ms.Id,m.Name,ms.BroadcastTime from MovieSession as ms 
                           inner join MovieInfo as m on m.Id=ms.MovieId";
            return Conn.Query<MovieSessionInfo>(sql);
        }

        public MovieSession GetSessionById(int id)
        {
            string sql = "select *from MovieSession where Id=@Id";
            return Conn.QuerySingleOrDefault<MovieSession>(sql, new { Id = id });
        }

        public bool Delete(int id)
        {
            string sql = "delete from MovieSession where Id=@Id";
            return Conn.Execute(sql, new { Id = id }) > 0;
        }
    }
}
